home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PEAR / Builder.php < prev    next >
PHP Script  |  2004-10-01  |  13KB  |  392 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Sµther Bakken <ssb@php.net>                            |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Builder.php,v 1.15 2004/03/04 13:37:37 chregu Exp $
  20.  
  21. require_once 'PEAR/Common.php';
  22.  
  23. /**
  24.  * Class to handle building (compiling) extensions.
  25.  *
  26.  * @author Stig Sµther Bakken <ssb@php.net>
  27.  */
  28. class PEAR_Builder extends PEAR_Common
  29. {
  30.     // {{{ properties
  31.  
  32.     var $php_api_version = 0;
  33.     var $zend_module_api_no = 0;
  34.     var $zend_extension_api_no = 0;
  35.  
  36.     var $extensions_built = array();
  37.  
  38.     var $current_callback = null;
  39.  
  40.     // used for msdev builds
  41.     var $_lastline = null;
  42.     var $_firstline = null;
  43.     // }}}
  44.     // {{{ constructor
  45.  
  46.     /**
  47.      * PEAR_Builder constructor.
  48.      *
  49.      * @param object $ui user interface object (instance of PEAR_Frontend_*)
  50.      *
  51.      * @access public
  52.      */
  53.     function PEAR_Builder(&$ui)
  54.     {
  55.         parent::PEAR_Common();
  56.         $this->setFrontendObject($ui);
  57.     }
  58.  
  59.     // }}}
  60.  
  61.     // {{{ _build_win32()
  62.  
  63.     /**
  64.      * Build an extension from source on windows.
  65.      * requires msdev
  66.      */
  67.     function _build_win32($descfile, $callback = null)
  68.     {
  69.         if (PEAR::isError($info = $this->infoFromDescriptionFile($descfile))) {
  70.             return $info;
  71.         }
  72.         $dir = dirname($descfile);
  73.         $old_cwd = getcwd();
  74.  
  75.         if (!@chdir($dir)) {
  76.             return $this->raiseError("could not chdir to $dir");
  77.         }
  78.         $this->log(2, "building in $dir");
  79.  
  80.         $dsp = $info['package'].'.dsp';
  81.         if (!@is_file("$dir/$dsp")) {
  82.             return $this->raiseError("The DSP $dsp does not exist.");
  83.         }
  84.         // XXX TODO: make release build type configurable
  85.         $command = 'msdev '.$dsp.' /MAKE "'.$info['package']. ' - Release"';
  86.  
  87.         $this->current_callback = $callback;
  88.         $err = $this->_runCommand($command, array(&$this, 'msdevCallback'));
  89.         if (PEAR::isError($err)) {
  90.             return $err;
  91.         }
  92.  
  93.         // figure out the build platform and type
  94.         $platform = 'Win32';
  95.         $buildtype = 'Release';
  96.         if (preg_match('/.*?'.$info['package'].'\s-\s(\w+)\s(.*?)-+/i',$this->_firstline,$matches)) {
  97.             $platform = $matches[1];
  98.             $buildtype = $matches[2];
  99.         }
  100.  
  101.         if (preg_match('/(.*)?\s-\s(\d+).*?(\d+)/',$this->_lastline,$matches)) {
  102.             if ($matches[2]) {
  103.                 // there were errors in the build
  104.                 return $this->raiseError("There were errors during compilation.");
  105.             }
  106.             $out = $matches[1];
  107.         } else {
  108.             return $this->raiseError("Did not understand the completion status returned from msdev.exe.");
  109.         }
  110.  
  111.         // msdev doesn't tell us the output directory :/
  112.         // open the dsp, find /out and use that directory
  113.         $dsptext = join(file($dsp),'');
  114.  
  115.         // this regex depends on the build platform and type having been
  116.         // correctly identified above.
  117.         $regex ='/.*?!IF\s+"\$\(CFG\)"\s+==\s+("'.
  118.                     $info['package'].'\s-\s'.
  119.                     $platform.'\s'.
  120.                     $buildtype.'").*?'.
  121.                     '\/out:"(.*?)"/is';
  122.  
  123.         if ($dsptext && preg_match($regex,$dsptext,$matches)) {
  124.             // what we get back is a relative path to the output file itself.
  125.             $outfile = realpath($matches[2]);
  126.         } else {
  127.             return $this->raiseError("Could not retrieve output information from $dsp.");
  128.         }
  129.         if (@copy($outfile, "$dir/$out")) {
  130.             $outfile = "$dir/$out";
  131.         }
  132.  
  133.         $built_files[] = array(
  134.             'file' => "$outfile",
  135.             'php_api' => $this->php_api_version,
  136.             'zend_mod_api' => $this->zend_module_api_no,
  137.             'zend_ext_api' => $this->zend_extension_api_no,
  138.             );
  139.  
  140.         return $built_files;
  141.     }
  142.     // }}}
  143.  
  144.     // {{{ msdevCallback()
  145.     function msdevCallback($what, $data)
  146.     {
  147.         if (!$this->_firstline)
  148.             $this->_firstline = $data;
  149.         $this->_lastline = $data;
  150.     }
  151.     // }}}
  152.  
  153.     // {{{ build()
  154.  
  155.     /**
  156.      * Build an extension from source.  Runs "phpize" in the source
  157.      * directory, but compiles in a temporary directory
  158.      * (/var/tmp/pear-build-USER/PACKAGE-VERSION).
  159.      *
  160.      * @param string $descfile path to XML package description file
  161.      *
  162.      * @param mixed $callback callback function used to report output,
  163.      * see PEAR_Builder::_runCommand for details
  164.      *
  165.      * @return array an array of associative arrays with built files,
  166.      * format:
  167.      * array( array( 'file' => '/path/to/ext.so',
  168.      *               'php_api' => YYYYMMDD,
  169.      *               'zend_mod_api' => YYYYMMDD,
  170.      *               'zend_ext_api' => YYYYMMDD ),
  171.      *        ... )
  172.      *
  173.      * @access public
  174.      *
  175.      * @see PEAR_Builder::_runCommand
  176.      * @see PEAR_Common::infoFromDescriptionFile
  177.      */
  178.     function build($descfile, $callback = null)
  179.     {
  180.         if (PEAR_OS == "Windows") {
  181.             return $this->_build_win32($descfile,$callback);
  182.         }
  183.         if (PEAR_OS != 'Unix') {
  184.             return $this->raiseError("building extensions not supported on this platform");
  185.         }
  186.         if (PEAR::isError($info = $this->infoFromDescriptionFile($descfile))) {
  187.             return $info;
  188.         }
  189.         $dir = dirname($descfile);
  190.         $old_cwd = getcwd();
  191.         if (!@chdir($dir)) {
  192.             return $this->raiseError("could not chdir to $dir");
  193.         }
  194.         $vdir = "$info[package]-$info[version]";
  195.         if (is_dir($vdir)) {
  196.             chdir($vdir);
  197.         }
  198.         $dir = getcwd();
  199.         $this->log(2, "building in $dir");
  200.         $this->current_callback = $callback;
  201.         $err = $this->_runCommand("phpize", array(&$this, 'phpizeCallback'));
  202.         if (PEAR::isError($err)) {
  203.             return $err;
  204.         }
  205.         if (!$err) {
  206.             return $this->raiseError("`phpize' failed");
  207.         }
  208.  
  209.         // {{{ start of interactive part
  210.         $configure_command = "$dir/configure";
  211.         if (isset($info['configure_options'])) {
  212.             foreach ($info['configure_options'] as $o) {
  213.                 list($r) = $this->ui->userDialog('build',
  214.                                                  array($o['prompt']),
  215.                                                  array('text'),
  216.                                                  array(@$o['default']));
  217.                 if (substr($o['name'], 0, 5) == 'with-' &&
  218.                     ($r == 'yes' || $r == 'autodetect')) {
  219.                     $configure_command .= " --$o[name]";
  220.                 } else {
  221.                     $configure_command .= " --$o[name]=".trim($r);
  222.                 }
  223.             }
  224.         }
  225.         // }}} end of interactive part
  226.  
  227.         // FIXME make configurable
  228.         if(!$user=getenv('USER')){
  229.             $user='defaultuser';
  230.         }
  231.         $build_basedir = "/var/tmp/pear-build-$user";
  232.         $build_dir = "$build_basedir/$info[package]-$info[version]";
  233.         $this->log(1, "building in $build_dir");
  234.         if (is_dir($build_dir)) {
  235.             System::rm("-rf $build_dir");
  236.         }
  237.         if (!System::mkDir("-p $build_dir")) {
  238.             return $this->raiseError("could not create build dir: $build_dir");
  239.         }
  240.         $this->addTempFile($build_dir);
  241.         if (getenv('MAKE')) {
  242.             $make_command = getenv('MAKE');
  243.         } else {
  244.             $make_command = 'make';
  245.         }
  246.         $to_run = array(
  247.             $configure_command,
  248.             $make_command,
  249.             );
  250.         if (!@chdir($build_dir)) {
  251.             return $this->raiseError("could not chdir to $build_dir");
  252.         }
  253.         foreach ($to_run as $cmd) {
  254.             $err = $this->_runCommand($cmd, $callback);
  255.             if (PEAR::isError($err)) {
  256.                 chdir($old_cwd);
  257.                 return $err;
  258.             }
  259.             if (!$err) {
  260.                 chdir($old_cwd);
  261.                 return $this->raiseError("`$cmd' failed");
  262.             }
  263.         }
  264.         if (!($dp = opendir("modules"))) {
  265.             chdir($old_cwd);
  266.             return $this->raiseError("no `modules' directory found");
  267.         }
  268.         $built_files = array();
  269.         while ($ent = readdir($dp)) {
  270.             if ($ent{0} == '.' || substr($ent, -3) == '.la') {
  271.                 continue;
  272.             }
  273.             // harvest!
  274.             if (@copy("modules/$ent", "$dir/$ent")) {
  275.                 $built_files[] = array(
  276.                     'file' => "$dir/$ent",
  277.                     'php_api' => $this->php_api_version,
  278.                     'zend_mod_api' => $this->zend_module_api_no,
  279.                     'zend_ext_api' => $this->zend_extension_api_no,
  280.                     );
  281.  
  282.                 $this->log(1, "$ent copied to $dir/$ent");
  283.             } else {
  284.                 chdir($old_cwd);
  285.                 return $this->raiseError("failed copying $ent to $dir");
  286.             }
  287.         }
  288.         closedir($dp);
  289.         chdir($old_cwd);
  290.         return $built_files;
  291.     }
  292.  
  293.     // }}}
  294.     // {{{ phpizeCallback()
  295.  
  296.     /**
  297.      * Message callback function used when running the "phpize"
  298.      * program.  Extracts the API numbers used.  Ignores other message
  299.      * types than "cmdoutput".
  300.      *
  301.      * @param string $what the type of message
  302.      * @param mixed $data the message
  303.      *
  304.      * @return void
  305.      *
  306.      * @access public
  307.      */
  308.     function phpizeCallback($what, $data)
  309.     {
  310.         if ($what != 'cmdoutput') {
  311.             return;
  312.         }
  313.         $this->log(1, rtrim($data));
  314.         if (preg_match('/You should update your .aclocal.m4/', $data)) {
  315.             return;
  316.         }
  317.         $matches = array();
  318.         if (preg_match('/^\s+(\S[^:]+):\s+(\d{8})/', $data, $matches)) {
  319.             $member = preg_replace('/[^a-z]/', '_', strtolower($matches[1]));
  320.             $apino = (int)$matches[2];
  321.             if (isset($this->$member)) {
  322.                 $this->$member = $apino;
  323.                 //$msg = sprintf("%-22s : %d", $matches[1], $apino);
  324.                 //$this->log(1, $msg);
  325.             }
  326.         }
  327.     }
  328.  
  329.     // }}}
  330.     // {{{ _runCommand()
  331.  
  332.     /**
  333.      * Run an external command, using a message callback to report
  334.      * output.  The command will be run through popen and output is
  335.      * reported for every line with a "cmdoutput" message with the
  336.      * line string, including newlines, as payload.
  337.      *
  338.      * @param string $command the command to run
  339.      *
  340.      * @param mixed $callback (optional) function to use as message
  341.      * callback
  342.      *
  343.      * @return bool whether the command was successful (exit code 0
  344.      * means success, any other means failure)
  345.      *
  346.      * @access private
  347.      */
  348.     function _runCommand($command, $callback = null)
  349.     {
  350.         $this->log(1, "running: $command");
  351.         $pp = @popen("$command 2>&1", "r");
  352.         if (!$pp) {
  353.             return $this->raiseError("failed to run `$command'");
  354.         }
  355.         if ($callback && $callback[0]->debug == 1) {
  356.             $olddbg = $callback[0]->debug;
  357.             $callback[0]->debug = 2;
  358.         }
  359.  
  360.         while ($line = fgets($pp, 1024)) {
  361.             if ($callback) {
  362.                 call_user_func($callback, 'cmdoutput', $line);
  363.             } else {
  364.                 $this->log(2, rtrim($line));
  365.             }
  366.         }
  367.         if ($callback && isset($olddbg)) {
  368.             $callback[0]->debug = $olddbg;
  369.         }
  370.         $exitcode = @pclose($pp);
  371.         return ($exitcode == 0);
  372.     }
  373.  
  374.     // }}}
  375.     // {{{ log()
  376.  
  377.     function log($level, $msg)
  378.     {
  379.         if ($this->current_callback) {
  380.             if ($this->debug >= $level) {
  381.                 call_user_func($this->current_callback, 'output', $msg);
  382.             }
  383.             return;
  384.         }
  385.         return PEAR_Common::log($level, $msg);
  386.     }
  387.  
  388.     // }}}
  389. }
  390.  
  391. ?>
  392.